package com.tang.k.wechatmessage.application.NewsList.view.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import com.tang.k.wechatmessage.application.NewsList.modle.INews;
import com.tang.k.wechatmessage.application.R;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by tangk on 2016/9/12.
 */
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.MyViewHolder>{
    private Context context;
    private List<INews> list;

    public ArticleAdapter(Context context, List<INews> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        holder.adMainTvAuthor.setText(list.get(position).getSource());
        holder.adMainTvTime.setText(list.get(position).getTimestamp());
        holder.adMainTitle.setText(list.get(position).getTitle());
        holder.adMainTvReadNum.setText(list.get(position).getReadNum() + "阅");
        holder.adMainTvCommentNum.setText(list.get(position).getCommentNum() + "评论");
        holder.adMainTvLikeNum.setText(list.get(position).getFavoriteNum() + "喜欢");
        Uri uri = Uri.parse(list.get(position).getFirstImg());
        holder.adMainImg.setImageURI(uri);

        if (itemClickListener != null) {
            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    itemClickListener.onClick(holder.linearLayout, pos);
                }
            });
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_main, parent, false));
        return holder;
    }

    /**
     * 使用ButterKnife实现DI
     */
    class MyViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.adMain_root)
        LinearLayout linearLayout;
        @InjectView(R.id.adMain_img)
        SimpleDraweeView adMainImg;
        @InjectView(R.id.adMain_tv_author)
        TextView adMainTvAuthor;
        @InjectView(R.id.adMain_tv_time)
        TextView adMainTvTime;
        @InjectView(R.id.adMain_title)
        TextView adMainTitle;
        @InjectView(R.id.adMain_tv_readNum)
        TextView adMainTvReadNum;
        @InjectView(R.id.adMain_tv_commentNum)
        TextView adMainTvCommentNum;
        @InjectView(R.id.adMain_tv_likeNum)
        TextView adMainTvLikeNum;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }

    }

    public ItemClickListener itemClickListener;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onClick(View view, int position);
    }
}
